package com.ruoyi.project.course.course.service.impl;

import java.util.List;

import com.ruoyi.common.utils.AddressLocationUtil;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.course.course.mapper.BCourseMapper;
import com.ruoyi.project.course.course.domain.BCourse;
import com.ruoyi.project.course.course.service.IBCourseService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 活动课程Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-24
 */
@Service
public class BCourseServiceImpl implements IBCourseService 
{
    @Autowired
    private BCourseMapper bCourseMapper;
    @Autowired
    AddressLocationUtil addressLocationUtil;

    /**
     * 查询活动课程
     * 
     * @param id 活动课程主键
     * @return 活动课程
     */
    @Override
    public BCourse selectBCourseById(Long id)
    {
        return bCourseMapper.selectBCourseById(id);
    }

    /**
     * 查询活动课程列表
     * 
     * @param bCourse 活动课程
     * @return 活动课程
     */
    @Override
    public List<BCourse> selectBCourseList(BCourse bCourse)
    {
        return bCourseMapper.selectBCourseList(bCourse);
    }

    /**
     * 新增活动课程
     * 
     * @param bCourse 活动课程
     * @return 结果
     */
    @Override
    public int insertBCourse(BCourse bCourse)
    {
        if(ObjectUtils.isEmpty(bCourse.getCoureseAddress())){
            throw new RuntimeException("详细地址未填写");
        }
        String lonAndLatByAddress = addressLocationUtil.getLonAndLatByAddress(bCourse.getCoureseAddress());
        if(ObjectUtils.isEmpty(lonAndLatByAddress)){
            throw new RuntimeException("详细地址转换经纬度失败");
        }
        String[] split = lonAndLatByAddress.split(",");
        bCourse.setCoureseLatitude(split[1]);
        bCourse.setCoureseLongitude(split[0]);
        bCourse.setCreateTime(DateUtils.getNowDate());
        bCourse.setCouresStatus(0);
        return bCourseMapper.insertBCourse(bCourse);
    }

    /**
     * 修改活动课程
     * 
     * @param bCourse 活动课程
     * @return 结果
     */
    @Override
    public int updateBCourse(BCourse bCourse)
    {
        if(ObjectUtils.isEmpty(bCourse.getCoureseAddress())){
            throw new RuntimeException("详细地址未填写");
        }
        String lonAndLatByAddress = addressLocationUtil.getLonAndLatByAddress(bCourse.getCoureseAddress());
        if(ObjectUtils.isEmpty(lonAndLatByAddress)){
            throw new RuntimeException("详细地址转换经纬度失败");
        }
        String[] split = lonAndLatByAddress.split(",");
        bCourse.setCoureseLatitude(split[1]);
        bCourse.setCoureseLongitude(split[0]);
        bCourse.setUpdateTime(DateUtils.getNowDate());
        return bCourseMapper.updateBCourse(bCourse);
    }

    /**
     * 批量删除活动课程
     * 
     * @param ids 需要删除的活动课程主键
     * @return 结果
     */
    @Override
    public int deleteBCourseByIds(String ids)
    {
        return bCourseMapper.deleteBCourseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除活动课程信息
     * 
     * @param id 活动课程主键
     * @return 结果
     */
    @Override
    public int deleteBCourseById(Long id)
    {
        return bCourseMapper.deleteBCourseById(id);
    }
}
